package org.langqiao.aliyun;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;

public class AliyunUploadFile{

    private static String endpoint = "oss-ap-southeast-5.aliyuncs.com";
    private static String accessKeyId = "LTAIdNypUqbwRxLc";
    private static String accessKeySecret = "RPxglfCpzIUs4lQczHadXD8sHwbWjK";
    
    private static String bucketName = "indonesia-object";
    
    private static String key = "test_aliyun.jpg";
    
    public static void downLoadFile(String userId,String fileName) throws IOException{
    	OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        System.out.println("Downloading an object");
        
        File file =new File("D:\\人脸识别样本数据\\"+userId);    
        //如果文件夹不存在则创建    
	    if(!file .exists() && !file.isDirectory()){       
	          System.out.println("//不存在");  
	          file .mkdir();    
	    } else{  
	          System.out.println("//目录存在");  
	    }  
        
        ObjectMetadata object = client.getObject(new GetObjectRequest(bucketName, "image/"+fileName),new File("D:\\人脸识别样本数据\\"+userId + "\\" + fileName));
        System.out.println("Content-Type: "  + object.getContentType());
        // 关闭client
        client.shutdown();
    }
    
    public static void main(String[] args) throws IOException {
    	
    	//downLoadFile("10006","20180224123402850326.jpg");
    	String fileName = "https://indonesia-object.oss-ap-southeast-5.aliyuncs.com/html/GiveData.html";
    	String[] arr = fileName.split("image/");
    	System.out.println(arr[1]);
    	OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        
        try {
            
             
            System.out.println("Uploading a new object to OSS from an input stream\n");
            String content = "Thank you for using Aliyun Object Storage Service";
            client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
            
             
            System.out.println("Uploading a new object to OSS from a file\n");
//          PutObjectResult uploadResult = client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
            PutObjectResult uploadResult = client.putObject(bucketName, key, new File("d:/GiveData.html"));
            String uploadUrl = uploadResult.getResponse().getUri();
            System.out.println("上传的url为..........：" + uploadUrl);
            
             
            System.out.println("Downloading an object");
            OSSObject object = client.getObject(new GetObjectRequest(bucketName, key));
            System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
            displayTextInputStream(object.getObjectContent());
            
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorCode());
            System.out.println("Error Code:       " + oe.getErrorCode());
            System.out.println("Request ID:      " + oe.getRequestId());
            System.out.println("Host ID:           " + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ce.getMessage());
        } finally {
            
             
            client.shutdown();
        }
    }
    
    private static File createSampleFile() throws IOException {
        File file = File.createTempFile("oss-java-sdk-", ".txt");
        file.deleteOnExit();

        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
        writer.write("abcdefghijklmnopqrstuvwxyz\n");
        writer.write("0123456789011234567890\n");
        writer.close();

        return file;
    }

    private static void displayTextInputStream(InputStream input) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        while (true) {
            String line = reader.readLine();
            if (line == null) break;

            System.out.println("\t" + line);
        }
        System.out.println();

        reader.close();
    }
}
